home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet multimedia / Animacje, filmy i prezentacje / Modelowanie 3D / K-3D 0.6.5.0 / k3d-all-in-one-setup-0.6.5.0.exe / k3d-setup-0.6.5.0.exe / share / shaders / k3d_raysphere.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-19  |  1.8 KB  |  59 lines

  1. /************************************************************************
  2.  * raysphere.h - intersect a ray with a sphere (this is a useful
  3.  *               solution to subproblems that crop up frequently)
  4.  *
  5.  * Author: Larry Gritz (gritzl@acm.org).
  6.  *
  7.  * Reference:
  8.  *   _Advanced RenderMan: Creating CGI for Motion Picture_, 
  9.  *   by Anthony A. Apodaca and Larry Gritz, Morgan Kaufmann, 1999.
  10.  *
  11.  * $Revision: 1.1 $    $Date: 2004/05/19 18:15:20 $
  12.  *
  13.  ************************************************************************/
  14.  
  15.  
  16. #ifndef RAYSPHERE_H
  17. #define RAYSPHERE_H 1
  18.  
  19.  
  20. /* raysphere - calculate the intersection of ray (E,I) with a sphere
  21.  * centered at the origin and with radius r.  We return the number of
  22.  * intersections found (0, 1, or 2), and place the distances to the
  23.  * intersections in t0, t1 (always with t0 <= t1).  Ignore any hits
  24.  * closer than eps.
  25.  */
  26. float
  27. raysphere (point E; vector I;   /* Origin and unit direction of the ray */
  28.            float r;             /* radius of sphere */
  29.        float eps;           /* epsilon - ignore closer hits */
  30.        output float t0, t1; /* distances to intersection */
  31.     )
  32. {
  33.     /* Set up a quadratic equation -- note that a==1 if I is normalized */
  34.     float b = 2 * ((vector E) . I);
  35.     float c = ((vector E) . (vector E)) - r*r;
  36.     float discrim = b*b - 4*c;
  37.     float solutions;
  38.     if (discrim > 0) {           /* Two solutions */
  39.     discrim = sqrt(discrim);
  40.     t0 = (-discrim - b) / 2;
  41.     if (t0 > eps) {
  42.         t1 = (discrim - b) / 2;
  43.         solutions = 2;
  44.     } else {
  45.         t0 = (discrim - b) / 2;
  46.         solutions = (t0 > eps) ? 1 : 0;
  47.     }
  48.     } else if (discrim == 0) {  /* One solution on the edge! */
  49.     t0 = -b/2;
  50.     solutions = (t0 > eps) ? 1 : 0;
  51.     } else {                    /* Imaginary solution -> no intersection */
  52.     solutions = 0;
  53.     }
  54.     return solutions;
  55. }
  56.  
  57.  
  58. #endif
  59.